﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class news_ModuleCalendarUpdate : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bd();
        }
    }
    /// <summary>
    /// 绑定数据
    /// </summary>
    private void bd()
    {
        int id = Convert.ToInt32(Request["id"]);
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        CMSModel.ModuleCalendar m = dal.ModuleCalendar.First(x => x.id == id);
        txtReason.Text = m.reason;
        txtTime.Text = m.ctime.ToShortDateString();
        MainQuoteDBModel.MainQuoteDBEntities stocks = DAL.DBManager.GetMainQuoteDbEntities();
        ddlModule.DataSource = stocks.TB_BK;
        ddlModule.DataTextField = "bk_name";
        ddlModule.DataValueField = "bk_code";
        ddlModule.DataBind();
        string y = stocks.TB_BK.First(x => x.BK_NAME.Contains(m.hotModule)).BK_CODE;
        ddlModule.SelectedValue = stocks.TB_BK.First(x => x.BK_NAME.Contains(m.hotModule)).BK_CODE;     //下拉框默认热门板块
        string t = ddlModule.SelectedValue;
        List<MainQuoteDBModel.TB_BK_GG> lstBK_GG = stocks.TB_BK_GG.Where(x => x.BK_CODE.Contains(t)).ToList();
        foreach (var bk in lstBK_GG)
        {
            var lst = CommonBll.DicStocks.First(x => x.Key.Contains(bk.GG_CODE));
            var name = lst.Key;
            var value = lst.Value;
            ListItem item = new ListItem(value, name);
            cblStock.Items.Add(item);
        }
        List<CMSModel.ModuleCalendarStocks> lstStock = dal.ModuleCalendarStocks.Where(x => x.fId == m.id).ToList();
        foreach (var x in lstStock)
        {
            txtStock.Text += x.stockname + ",";
            hidSelectValue.Value += x.stock + ",";
        }

    }
    /// <summary>
    /// 更新索引后更新弹出层的数据绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
    {
        string t = ddlModule.SelectedValue;
        MainQuoteDBModel.MainQuoteDBEntities stocks = DAL.DBManager.GetMainQuoteDbEntities();
        List<MainQuoteDBModel.TB_BK_GG> lstBK_GG = stocks.TB_BK_GG.Where(x => x.BK_CODE.Contains(t)).ToList();
        foreach (var bk in lstBK_GG)
        {
            var lst = CommonBll.DicStocks.First(x => x.Key.Contains(bk.GG_CODE));
            var name = lst.Key;
            var value = lst.Value;
            ListItem item = new ListItem(value, name);
            cblStock.Items.Add(item);
        }
    }
    /// <summary>
    /// 更新当日热门板块及可关注股票
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(Request["id"]);
            CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
            CMSModel.ModuleCalendar m = dal.ModuleCalendar.First(x => x.id == id);
            //循环删除之前板块有关事件
            List<CMSModel.ModuleCalendarEvents> lstEvent = dal.ModuleCalendarEvents.Where(x => x.fId == m.id).ToList();
            foreach (var n in lstEvent)
            {
                dal.DeleteObject(new CMSModel.ModuleCalendarEvents() { fId = m.id });
            }
            m.hotModule = ddlModule.SelectedItem.ToString();
            m.reason = txtReason.Text;
            m.ModuleId = ddlModule.SelectedValue;
            DateTime dt;
            string date = txtTime.Text;
            DateTime.TryParse(date, out dt);
            m.ctime = dt;    //日期
            string stocks = hidSelectValue.Value;
            string stockname = txtStock.Text;
            //获取热门板块中所有可关注股票
            List<CMSModel.ModuleCalendarStocks> lstStock = dal.ModuleCalendarStocks.Where(x => x.fId == m.id).ToList();
            MainQuoteDBModel.MainQuoteDBEntities s = DAL.DBManager.GetMainQuoteDbEntities();
            //删除之前股票
            foreach (var t in lstStock)
            {
                dal.DeleteObject(t);
            }
            //添加至股票表
            foreach (var x in stocks.Split(','))
            {
                if (!string.IsNullOrEmpty(x))
                {
                    //根据股票得到股票名称
                    string y = "";
                    if (s.TB_GGXX.Any(c => c.G_CODE == x))
                    {
                        y = s.TB_GGXX.First(c => c.G_CODE.Contains(x)).G_NAME;
                    }
                    else
                    {
                        y = x;
                    }
                    dal.AddToModuleCalendarStocks(new CMSModel.ModuleCalendarStocks() { stock = x, stockname = y, fId = m.id });
                }

            }
            //循环添加入板块事件表
            var lstNews = dal.News.Where(x => x.keyword.Contains(m.hotModule) && x.ptime <= DateTime.Now && x.IsEnable == true).Select(x => new { x.title, x.id, x.ptime }).OrderByDescending(x => x.ptime).Take(5).ToList();
            if (lstNews.Count > 0)
            {
                foreach (var n in lstNews)
                {
                    dal.AddToModuleCalendarEvents(new CMSModel.ModuleCalendarEvents() { newsId = n.id, fId = id });
                }
            }
            dal.SaveChanges();
            ClientScript.RegisterStartupScript(GetType(), "update", "alert('修改成功');document.location='ModuleCalendar.aspx'", true);
        }
        catch (Exception)
        {
            ClientScript.RegisterStartupScript(GetType(), "update", "alert('修改失败');", true);

        }

    }
}